package org.example.payment.controller;

import org.example.common.Response;
import org.example.payment.entity.vo.WechatPayVO;
import org.example.payment.service.IWechatPayService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * H5支付控制层
 */
@RestController
@RequestMapping("/api/H5pay")
public class H5PayController {

    @Resource
    private IWechatPayService wechatPayService;

    /**
     * 微信H5支付
     *
     * @param wechatPayVO 微信支付参数
     * @return 微信H5支付二维码
     */
    @PostMapping("/wechat")
    public Response<String> wechatPay(@RequestBody WechatPayVO wechatPayVO) {
        try {
            return Response.success(wechatPayService.h5Pay(wechatPayVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("微信支付失败");
        }
    }

    /**
     * 微信支付回调
     * @param requestBody
     * @param serial
     * @param nonce
     * @param signature
     * @param timestamp
     * @return
     */
    @PostMapping("/notify")
    public Response<String> handleNotify(@RequestBody String requestBody,
                                         @RequestHeader("Wechatpay-Serial") String serial,
                                         @RequestHeader("Wechatpay-Nonce") String nonce,
                                         @RequestHeader("Wechatpay-Signature") String signature,
                                         @RequestHeader("Wechatpay-Timestamp") String timestamp
                                         ) {

        return wechatPayService.handleNotify(requestBody, serial, nonce, signature, timestamp);
    }


}
